<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdapterWebController;
use App\Modules\Sp\Sp;
use App\Models\Sale\Sale;
use App\Models\Sp\Sp as SpModel;
use Illuminate\Http\Request;
use phpDocumentor\Reflection\DocBlock\Tags\Throws;


class SpController extends AdapterWebController
{

    /**
     * 添加sp
     */
    public function addSp(Request $request)
    {
        if ($request->isMethod('get')) {
            $saleModel = new Sale();
            $saleList = $saleModel->get();
            if(!empty($saleList)){
                $saleList = $saleList->toArray();
            }
            return view('admin.music.addsp', ['saleList' => $saleList]);
        }
        $file1Res = $this->fileUpload($request, 'agree_record');
        if (!$file1Res['status']) {
            throw_exception('请上传协议备案');
        }
        $file2Res = $this->fileUpload($request,'document_record');

        if (!$file2Res['status']) {
            $this->delFileByPath($file1Res['url']);
            throw_exception('请上传证件备案');
        }

        $postData = array_filter($request->except('_token'));
        $postData['agree_record'] = $file1Res['url'];
        $postData['document_record'] = $file2Res['url'];
        $postData['check_man'] = session('username');
        $res = $this->appLayerExec(Sp::class, 'addSp', $postData);

        if ($res['status'] != 1) {
            $this->delFileByPath($file1Res['url']);
            $this->delFileByPath($file2Res['url']);
        }
        return view('admin.jump')->with([
            'message'=>'提交成功！',
            'url' =>url('/admin/addSp'),
            'jumpTime'=>2,
        ]);
    }

    //sp列表
    public function spList(Request $request){
        $postData = $request->all();
        $res = $this->appLayerExec(Sp::class, 'spList', $postData);
        $saleModel = new Sale();
        $saleList = $saleModel->get();
        if(!empty($saleList)){
            $saleList = $saleList->toArray();
        }
        return view('admin.music.sp_list',['data' => $res['res'], 'saleList' => $saleList]);
    }

    //查看sp信息弹出层
    public function spPop ($id)
    {
        $postData['id'] = $id;
        $res = $this->appLayerExec(Sp::class, 'spDetail', $postData);
        return view('admin.music.sp_pop', ['data' => $res['res'][0]]);
    }

    //sp详情
    public function spDetail($id){
        $postData['id'] = $id;
        $this->appLayerExec(Sp::class, 'spDetail', $postData);
    }

    //修改sp
    public function updateSp(Request $request,$id){
        if ($request->isMethod('get')) {
            $postData['id'] = $id;
            $res = $this->appLayerExec(Sp::class, 'spDetail', $postData);
            $mainSale = Sale::where('type','=',1)->get();
            if(!empty($mainSale)){
                $mainSale = $mainSale->toArray();
            }
            $assistSale = Sale::where('type','=',2)->get();
            if(!empty($assistSale)){
                $assistSale = $assistSale->toArray();
            }
            return view('admin.music.edit_sp', ['data' => $res['res'][0],'mainSale'=>$mainSale,'assistSale'=>$assistSale]);
        }

        $postData = $request->except('_token');
        $postData['id'] = $id;
        $spModel = new SpModel();
        $spData = $spModel->find($id);

        $postData['memory_day'] = [];
        if(!empty($postData['memory_day1']))
            $postData['memory_day'][] = ['day' => $postData['memory_day1'], 'des' => $postData['memory_des1']];

        if(!empty($postData['memory_day2']))
            $postData['memory_day'][] = ['day' => $postData['memory_day2'], 'des' => $postData['memory_des2']];

        if(!empty($postData['memory_day3']))
            $postData['memory_day'][] = ['day' => $postData['memory_day3'], 'des' => $postData['memory_des3']];

        $postData['memory_day'] = serialize($postData['memory_day']);
        unset($postData['memory_day1']);
        unset($postData['memory_des1']);
        unset($postData['memory_day2']);
        unset($postData['memory_des2']);
        unset($postData['memory_day3']);
        unset($postData['memory_des3']);

        foreach ($postData as $k => $v) {
            if($v)
                $spData->{$k} = $v;
        }
        $res = $spData->save();
        if ($res) {
            flash('更新成功')->success();
            return redirect()->back();
        }else{
            throw_exception('更新失败');
        }
    }

    //更新协议
    public function updateAgreement (Request $request, $id)
    {

        if ($request->isMethod('get')) {
            $spData = $this->appLayerExec(Sp::class, 'spDetail', ['id' => $id]);
            return view('admin.music.update_agreement', ['data' => $spData['res'][0]]);
        }

        $spModel = new SpModel();
        $spData = $spModel->find($id);
        $data = $request->except('_token');
        if (!$data['fix_date'] || !$data['fix_choose']) {
            flash('签约时间和约期必选')->error();
            return redirect()->back();
        }

        $spData->fix_date = $data['fix_date'];
        $spData->fix_choose = $data['fix_choose'];

        //执行上传并更新数据库
        $uploadRes = $this->fileUpload($request, 'agree_record');
        if ($uploadRes['status']) {
            //删除原来协议
            $this->delFileByPath($spData->agree_record);

            $spData->agree_record = $uploadRes['url'];
            if ($spData->save()) {
                flash('更新协议成功')->success();
                return redirect()->back();
            } else {
                flash('更新协议失败')->error();
                return redirect()->back();
            }
        } else {
            flash('上传协议失败')->error();
            return redirect()->back();
        }
    }

    //终止/恢复协议
    public function termination(Request $request, $id)
    {
        $postData = $request->all();
        $postData['id'] = $id;

        $res = $this->appLayerExec(Sp::class, 'termination', $postData);
        return $res;
    }
}